/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean;

import assisisw.smartcal.entitysubsystem.IUserController;
import assisisw.smartcal.entitysubsystem.User;
import assisisw.smartcal.entitysubsystem.exceptions.user.UserNotRegisteredException;
import assisisw.smartcal.securitysubsystem.IMailVerifier;
import assisisw.smartcal.securitysubsystem.PasswordManager;
import assisisw.smartcal.utilities.CommunicationException;
import assisisw.smartcal.utilities.Configuration;
import javax.inject.Named;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Francesco
 */
@ManagedBean ()
@Named ( value = "signInBean" )
@ViewScoped ()
public class SignInBean implements Serializable
{

    // Static Fields
  
    /***/
    private static final String LOGIN_INCORRECT_MESSAGE = " Sorry, username / password incorrect. Please, retry or sign up !" ;
    
    /***/
    private static final String USER_NOT_ACTIVATED_MESSAGE = "You have not activated your account yet !\nWe just sent another token to your mail %s\nPlease, follow the link in that mail and then sign in again !" ;
     
    // Instance Fields
    
    /***/
    @EJB
    private IUserController userController ;
    
    @EJB
    private IMailVerifier mailVerifier ;
    
    /***/
    private PasswordManager passwordManager ;
    
    /***/
    private User userSigningIn ;
        
    /***/
    private String errorMessage ;
    
   // Instance Methods
    
    /***/
    public SignInBean () {}
    
    /***/
    @PostConstruct
    public void init () 
    {
        userSigningIn = new User () ;
        errorMessage = "" ;
        passwordManager = new PasswordManager() ;
    }
    
    /**
     * @return
     */
    public User getUserSigningIn () 
    {
        return userSigningIn ;
    }
    
    /**
     * @return
     */
    public String signIn () 
    {
        // what if the user has already logged in ? -> approfondire sessions !
        User u ;
        String res ;
        String token ;
        String passwordToCheck ;
        try 
        {
            u = userController.getUser ( userSigningIn.getMail() ) ;
            passwordToCheck = passwordManager.hash( userSigningIn.getPassword () ) ;
            if ( passwordToCheck.equals ( u.getPassword() ) )
            {
                if ( u.isActive () )
                {
                    res = Configuration.Page.DASHBOARD.getValue () ;
                    FacesContext.getCurrentInstance ().getAttributes().put ( "mail" , userSigningIn.getMail () ) ;
                }
                else
                {
                    token = mailVerifier.sendToken ( userSigningIn.getMail () ) ;
                    res = Configuration.Page.INDEX.getValue () ;
                    errorMessage = String.format ( USER_NOT_ACTIVATED_MESSAGE , userSigningIn.getMail () ) ;
                    u.setToken(token);
                    userController.update(u); 
                    FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( errorMessage ) ) ;
                }
            }
            else
            {
                errorMessage = LOGIN_INCORRECT_MESSAGE ;
                res = Configuration.Page.INDEX.getValue () ;
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage ( errorMessage ));
            }
        } 
        catch ( UserNotRegisteredException ex ) 
        {
            errorMessage = LOGIN_INCORRECT_MESSAGE ;
            res = Configuration.Page.INDEX.getValue () ;
            FacesContext.getCurrentInstance().addMessage ( null , new FacesMessage ( errorMessage ) ) ;
        } 
        catch ( CommunicationException ex ) 
        {
            errorMessage = "Unknown Error !" ;
            res = Configuration.Page.UNKNOWN_ERROR.getValue () ;
        } 
        return res ;
    }
 
}
